//
//  YXChangeCodeViewController.m
//  跑跑快销
//
//  Created by 杨潇 on 14-4-30.
//  Copyright (c) 2014年 ylife. All rights reserved.
//

#import "YXChangeCodeViewController.h"

@interface YXChangeCodeViewController ()

@end

@implementation YXChangeCodeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _changeCodeVi = [[YXChangeCodeView alloc] init];
    self.view = _changeCodeVi;
    
    [_changeCodeVi.getCodeBtn addTarget:self action:@selector(touchGetCodeBtn) forControlEvents:UIControlEventTouchUpInside];
    [_changeCodeVi.affirmBtn addTarget:self action:@selector(touchAffirmBtn) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - ButtonsClick
- (void)touchGetCodeBtn
{
    [self.view endEditing:YES];
    if ([_changeCodeVi.phoneNumberTF.text isEqualToString:@""]) {
        [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"请输入手机号码。"];
        return;
    }
    if (_changeCodeVi.phoneNumberTF.text.length != 11) {
        [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"请输入正确的手机号码."];
        return;
    }
    [NSURLConnection sendRequestWithHeadStr:[[YXWindowInfo sharedWindowInfo] requestUrlStr] headValue:nil serviceToken:nil text:[NSDictionary dictionaryWithObjectsAndKeys:@"resetCode",@"type",_changeCodeVi.phoneNumberTF.text,@"phone", nil] dataBlock:^(NSDictionary *dic) {
        NSString * resultCode = [dic objectForKey:@"resultCode"];
        if ([resultCode isEqualToString:@"0"]) {
            [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"获取验证码成功，请注意手机接收。"];
        }else{
            [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:[dic objectForKey:@"errorDesc"]];
        }
    }];
}

- (void)touchAffirmBtn
{
    [self.view endEditing:YES];
    if ([_changeCodeVi.phoneNumberTF.text isEqualToString:@""]) {
        [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"请输入手机号码。"];
        return;
    }
    if (_changeCodeVi.phoneNumberTF.text.length != 11) {
        [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"请输入正确的手机号码."];
        return;
    }
    if ([_changeCodeVi.codeTF.text isEqualToString:@""]) {
        [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"请输入验证码。"];
        return;
    }
    if ([_changeCodeVi.changeCodeTF.text isEqualToString:@""]) {
        [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"请输入激活码。"];
        return;
    }
    [NSURLConnection sendRequestWithHeadStr:[[YXWindowInfo sharedWindowInfo] requestUrlStr] headValue:nil serviceToken:nil text:[NSDictionary dictionaryWithObjectsAndKeys:@"resetActiveCode",@"type",_changeCodeVi.phoneNumberTF.text,@"phone",_changeCodeVi.changeCodeTF.text,@"Activation",_changeCodeVi.codeTF.text,@"code", nil] dataBlock:^(NSDictionary *dic) {
        NSString * resultCode = [dic objectForKey:@"resultCode"];
        if ([resultCode isEqualToString:@"0"]) {
            [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:@"修改激活码成功。"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [[YXWindowAlertView sharedWindowAlert] showLableWithTitle:[dic objectForKey:@"errorDesc"]];
        }
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
